package com.zcn.io.web.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zcn.io.common.entity.OrderMain;
import com.zcn.io.common.entity.TransferPhone;
import com.zcn.io.common.pojo.CommonResult;
import com.zcn.io.common.util.DateUtil;
import com.zcn.io.web.api.entity.OrderTransferPhoneRecord;
import com.zcn.io.web.mapper.OrderTransferPhoneRecordMapper;
import lombok.Data;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @desc:
 * @author: 202438 邱建伟
 * @date: 2024/10/4 16:28
 * @Copyright (c) 2024, www.zcn.com Inc. All Rights Reserved.
 */
@RequiredArgsConstructor
@Service
public class OrderTransferPhoneRecordService {

    private final OrderTransferPhoneRecordMapper mapper;

    private final TransferPhoneService transferPhoneService;

    public List<OrderTransferPhoneRecord> getAllByOrderId(String orderId) {
        LambdaQueryWrapper<OrderTransferPhoneRecord> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(OrderTransferPhoneRecord::getOrderId, orderId);
        wrapper.orderByAsc(OrderTransferPhoneRecord::getId);
        return mapper.selectList(wrapper);
    }

    public void save(OrderMain orderMain) {
        TransferPhone transferPhone = transferPhoneService.getById(orderMain.getTransferPhone());
        if (transferPhone != null){
            OrderTransferPhoneRecord record = new OrderTransferPhoneRecord();
            record.setOrderId(orderMain.getId());
            record.setPhoneCode(transferPhone.getIndexId());
            record.setPhoneNo(transferPhone.getPhoneNo());
            //生成当前日期：yyyy-MM-dd HH:mm:ss
            record.setChangeDate(DateUtil.getDefaultDate());
            this.mapper.insert(record);
        }
    }

    public void deleteById(String id) {
        this.mapper.deleteById(id);
    }
}
